package com.yj.controller;

import com.yj.entity.ReturnT;
import com.yj.service.SSEService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@Controller
@Slf4j
@RequestMapping ()
public class SSEController {


    @Autowired
    private SSEService sseService;

    @GetMapping("/")
    public String index(){
        return "index";
    }

    @GetMapping("/subscribe")
    public SseEmitter subscribe(String userId){
        return sseService.subscribe(userId);
    }

    @GetMapping("/publish")
    @ResponseBody
    public ReturnT publish(String userId, String message){
        sseService.publish(userId, message);
        return ReturnT.success();
    }

    @GetMapping("/unsubscribe")
    public void unsubscribe(String userId){
        sseService.unsubscribe(userId);
    }

}
